package com.nl.controller.xuyi.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.controller.TimedTask;
import com.nl.controller.web.service.LoginService;
import com.nl.controller.web.service.SmsUserService;
import com.nl.controller.xuyi.bean.XuYiCheckInfoBo;
import com.nl.controller.xuyi.bean.XuYiContentTemplateInfo;

import com.nl.controller.xuyi.mapper.XuYiCheckMapper;
import com.nl.controller.xuyi.mapper.XuYiContentTemplateMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @desc 短信模板业务逻辑处理
 * @return null
 * @author zhaohz
 * @date 2020/9/8 17:48
 */
@Service
@Slf4j
public class XuYiContentTemplateService {

    static final String CONTENT_ADD = "add";
    static final String CONTENT_MODIFY = "modify";
    static final String ZERO = "0";

    @Autowired
    private XuYiContentTemplateMapper xuYiContentTemplateMapper;

    @Autowired
    private XuYiCheckMapper xuYiCheckMapper;

    @Autowired
    private LoginService loginService;

    @Autowired
    private SmsUserService smsUserService;

    @Autowired
    private CommonService commonService;

    @SystemServiceLog(description = "按照条件查询短信模板的信息", value = false)
    public PageInfo<XuYiContentTemplateInfo> findContentTemplate(int page, int limit, String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("contentName", name);
        PageHelper.startPage(page, limit);
        List<XuYiContentTemplateInfo> list = new ArrayList<>();
        try {
            list = xuYiContentTemplateMapper.findContentTemplate(map);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询短信模板发生异常："+e.toString());
        }
        PageInfo<XuYiContentTemplateInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @SystemServiceLog(description = "更改短信模板表", value = false)
    public int configureContentTemplate(String flag,String contentId,String userMsisdn,String contentName,String contentContent,String createTime,String modifyTime,String contentState,String auditState,String contentNameChange){
        int result = 0;
        String checkNumber = "1";
        List<SmsUsers> cityUsers =smsUserService.findCityUsersbyCityId("12");

        XuYiCheckInfoBo xuYiCheckInfoBo = new XuYiCheckInfoBo();
        XuYiContentTemplateInfo xuYiContentTemplateInfo =new XuYiContentTemplateInfo();
        xuYiContentTemplateInfo.setContentId(contentId);
        xuYiContentTemplateInfo.setUserMsisdn(userMsisdn);
        xuYiContentTemplateInfo.setContentName(contentName);
        xuYiContentTemplateInfo.setContentContent(contentContent);
        xuYiContentTemplateInfo.setCreateTime(createTime);
        xuYiContentTemplateInfo.setModifyTime(modifyTime);
        xuYiContentTemplateInfo.setContentState(contentState);
        xuYiContentTemplateInfo.setAuditState(auditState);
        int countName=0;
        String contentTempId = "";
        try {
        /*
        添加模板
         */
            if (CONTENT_ADD.equals(flag)) {
                countName= xuYiContentTemplateMapper.findContentTemplateNumByContentName(contentName).size();
                /*
                名称重复
                 */
        if(countName>0){
            result = 2;
        }else{
            //result = xuYiContentTemplateMapper.insertContentTemplate(xuYiContentTemplateInfo);
            contentTempId = xuYiContentTemplateMapper.findContentTempSeq();
            xuYiCheckInfoBo.setType("1");
            xuYiCheckInfoBo.setConTempId(contentTempId);
            xuYiCheckInfoBo.setConTemp(contentContent);
            xuYiCheckInfoBo.setCheckState("0");
            xuYiCheckInfoBo.setOperator(userMsisdn);
            xuYiCheckInfoBo.setConTempName(contentName);
            xuYiCheckInfoBo.setCheckNumber("0");
            result = xuYiCheckMapper.addConTempCheckInfo(xuYiCheckInfoBo);

            commonService.createSmsOperLog("2", "新增短信模板【id:"+contentTempId +"】", "0", "短信模板管理");

            for(SmsUsers cityUser: cityUsers){
                loginService.sendCode(cityUser.getMsisdn(),"【江苏省应急平台】您好，盱眙供电局操作员"+userMsisdn+"，新增了短信模板，模板编码："+contentTempId+"，模板名称："+contentName+"，" +
                        "模板内容："+contentContent+"。审核通过请回复\'Y-"+contentTempId+"+"+checkNumber+"-XYMB\'，审核不通过请回复\'N-"+contentTempId+"+"+checkNumber+"-XYMB\'。");
            }
        }
    }else if(CONTENT_MODIFY.equals(flag)){
                //没改名字
                if(ZERO.equals(contentNameChange)){
                    //result = xuYiContentTemplateMapper.modifyContentTemplate(xuYiContentTemplateInfo);
                    xuYiCheckInfoBo.setType("2");
                    xuYiCheckInfoBo.setConTempId(contentId);
                    xuYiCheckInfoBo.setConTempName(contentName);
                    xuYiCheckInfoBo.setConTemp(contentContent);
                    xuYiCheckInfoBo.setCheckState("0");
                    xuYiCheckInfoBo.setOperator(userMsisdn);

                    XuYiContentTemplateInfo xuYiContentTemplateInfo1  = xuYiContentTemplateMapper.findContentTemplateByContentId(contentId);
                    xuYiCheckInfoBo.setCheckNumber(xuYiContentTemplateInfo1.getCheckNumber());
                    result =xuYiCheckMapper.addConTempCheckInfo(xuYiCheckInfoBo);
                    int checkNumber2 = Integer.parseInt(xuYiContentTemplateInfo1.getCheckNumber())+1;
                    for(SmsUsers cityUser: cityUsers){
                        loginService.sendCode(cityUser.getMsisdn(),"【江苏省应急平台】您好，盱眙供电局操作员"+userMsisdn+"，修改了短信模板，模板编码："+contentId+"，模板名称："+contentName+"，" +
                                "模板内容："+contentContent+"。审核通过请回复\'Y-"+contentId+"+"+checkNumber2+"-XYMB\'，审核不通过请回复\'N-"+contentId+"+"+checkNumber2+"-XYMB\'。");
                    }
                    //删除占位短信
                    TimedTask.map.remove(contentId+"+"+checkNumber2+"DSXYMB");
                    TimedTask.map.remove(contentId+"+"+checkNumber2+"LDXYMB");

                    commonService.createSmsOperLog("3", "修改短信模板【id:"+contentId +"】", "0", "短信模板管理");

                }else {
                    //目前不支持修改名字
                    //修改名字
                    countName= xuYiContentTemplateMapper.findContentTemplateNumByContentName(contentName).size();
                    /*
                    名称重复
                     */
                    if(countName>0){
                        result = 2;
                    }else {
                        //result = xuYiContentTemplateMapper.modifyContentTemplate(xuYiContentTemplateInfo);
                        xuYiCheckInfoBo.setType("1");
                        xuYiCheckInfoBo.setConTempId(contentId);
                        xuYiCheckInfoBo.setConTemp(contentContent);
                        xuYiCheckInfoBo.setCheckState("0");
                        xuYiCheckInfoBo.setOperator(userMsisdn);
                        result = xuYiCheckMapper.addConTempCheckInfo(xuYiCheckInfoBo);
                        for(SmsUsers cityUser: cityUsers){
                            loginService.sendCode(cityUser.getMsisdn(),"【江苏省应急平台】您好，盱眙供电局操作员"+userMsisdn+"，新增了短信模板，模板编码："+contentId+"，模板名称："+contentName+"，" +
                                    "模板内容："+contentContent+"。审核通过请回复\'Y-"+contentId+"+"+checkNumber+"-XYMB\'，审核不通过请回复\'N-"+contentId+"+"+checkNumber+"-XYMB\'。");
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("编辑短信模板发生异常："+e.toString());
            if(CONTENT_ADD.equals(flag)){
                commonService.createSmsOperLog("2", "新增短信模板【id:"+contentTempId +"】", "1", "短信模板管理");
            }else if(CONTENT_MODIFY.equals(flag)){
                commonService.createSmsOperLog("3", "修改短信模板【id:"+contentId +"】", "1", "短信模板管理");
            }
        }
        return result;
    }


    @SystemServiceLog(description = "删除短信模板", value = false)
    public int deleteContentTemplate(String contentId,String operatorTel) {
        int result = 0;
        XuYiCheckInfoBo xuYiCheckInfoBo = new XuYiCheckInfoBo();
        XuYiContentTemplateInfo xuYiContentTemplateInfo;
        String tempContent;
        try {
            //直接删除
            //result = xuYiContentTemplateMapper.deleteContentTemplate(contentId);

            //入审核表
            xuYiContentTemplateInfo = xuYiContentTemplateMapper.findContentTemplateByContentId(contentId);
            tempContent = xuYiContentTemplateInfo.getContentContent();
            xuYiCheckInfoBo.setType("3");
            xuYiCheckInfoBo.setOperator(operatorTel);
            xuYiCheckInfoBo.setConTempId(contentId);
            xuYiCheckInfoBo.setConTemp(tempContent);
            xuYiCheckInfoBo.setConTempName(xuYiContentTemplateInfo.getContentName());
            xuYiCheckInfoBo.setCheckState("0");
            xuYiCheckInfoBo.setCheckNumber("0");
            result = xuYiCheckMapper.addConTempCheckInfo(xuYiCheckInfoBo);
            commonService.createSmsOperLog("5", "删除短信模板【id:"+contentId +"】", "0", "短信模板管理");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除短信模板发生异常："+e.toString());
            commonService.createSmsOperLog("5", "删除短信模板【id:"+contentId +"】", "1", "短信模板管理");
        }
        return result;
    }


    @SystemServiceLog(description = "根据短信模板编号查询模板信息", value = false)
    public XuYiContentTemplateInfo findContentTemplateByContentId(String contentId) {
        XuYiContentTemplateInfo xuYiContentTemplateInfo = null;
        try {
            xuYiContentTemplateInfo = xuYiContentTemplateMapper.findContentTemplateByContentId(contentId);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("根据短信模板编号查询模板信息异常："+e.toString());
        }
        return xuYiContentTemplateInfo;
    }

    public List<XuYiContentTemplateInfo> findContentTempById(String contentId) {
        XuYiContentTemplateInfo xuYiContentTemplateInfo = new XuYiContentTemplateInfo();
        xuYiContentTemplateInfo.setContentId(contentId);
        List<XuYiContentTemplateInfo> contentTempList = xuYiContentTemplateMapper.findContentTempById(xuYiContentTemplateInfo);
        return  contentTempList;

    }
}
